package com.noah.note.ui;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.noah.note.R;
import com.noah.note.adapter.NoteAdapter;
import com.noah.note.bean.ReplyNote;
import com.noah.note.global.BaseFragment;
import com.noah.note.global.Const;
import com.noah.note.presenter.NotePresenter;
import com.noah.note.utils.HandlerUtils;
import com.noah.note.utils.LogUtils;
import com.noah.note.view.NoteView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by zzh on 2018/5/16.
 */
public class NoteListFragment extends BaseFragment<NotePresenter> implements NoteView {
    @BindView(R.id.rvNote)
    RecyclerView rvNote;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    Unbinder unbinder;
    private NoteAdapter noteAdapter;
    private static final int MSG_UPDATE_LIST = 1;
    private static final int MSG_NEXT_DATA = 2;

    @Override
    protected NotePresenter initPresenter() {
        return new NotePresenter();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_note_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        getData(System.currentTimeMillis(), false);
        return view;
    }

    private void initView() {
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity());
        rvNote.setLayoutManager(manager);
        noteAdapter = new NoteAdapter(R.layout.item_note, new ArrayList<ReplyNote>());
        rvNote.setAdapter(noteAdapter);
        //下拉刷新
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(false);//下拉图标消失
                getData(System.currentTimeMillis(), true);
            }
        });
        //加载更多
        noteAdapter.setEnableLoadMore(true);
        noteAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                LogUtils.i("onLoadMoreRequested ");
                HandlerUtils.sendMessageDelay(mHandler, MSG_NEXT_DATA, 500);
            }
        }, rvNote);
    }

    public void getData(long time, boolean isRefresh) {
        HandlerUtils.removeHandlerMsg(mHandler, MSG_NEXT_DATA);
        mPresenter.getNoteList(0, time, isRefresh);
        LogUtils.i("time = " + time + ",isRefresh=" + isRefresh);
    }

    @Override
    public void noteListSuccess(List<ReplyNote> replyNotes) {
        if (replyNotes != null) {
            HandlerUtils.sendMessage(mHandler, MSG_UPDATE_LIST, replyNotes, Const.ARG1_IS_NORMAL_LOAD, Const.ARG2_IS_LOAD_SUCCESS);
        } else {
            HandlerUtils.sendMessage(mHandler, MSG_UPDATE_LIST, replyNotes, Const.ARG1_IS_NORMAL_LOAD, Const.ARG2_IS_LOAD_FAIL);
        }
    }

    @Override
    public void noteListRefreshSuccess(List<ReplyNote> replyNotes) {
        if (replyNotes != null) {
            HandlerUtils.sendMessage(mHandler, MSG_UPDATE_LIST, replyNotes, Const.ARG1_IS_REFRESH_LOAD, Const.ARG2_IS_LOAD_SUCCESS);
        } else {
            HandlerUtils.sendMessage(mHandler, MSG_UPDATE_LIST, replyNotes, Const.ARG1_IS_REFRESH_LOAD, Const.ARG2_IS_LOAD_FAIL);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void handlerMessage(Message msg) {
        super.handlerMessage(msg);
        switch (msg.what) {
            case MSG_UPDATE_LIST:
                if (msg.arg2 == Const.ARG2_IS_LOAD_FAIL) {//没有获取到数据,加载完毕
                    noteAdapter.loadMoreEnd();
                    return;
                }
                List<ReplyNote> replyNotes = (List<ReplyNote>) msg.obj;
                LogUtils.i("MSG_UPDATE_LIST " + replyNotes);
                if (replyNotes.size() < 10) {
                    noteAdapter.loadMoreEnd();
                } else {
                    noteAdapter.loadMoreComplete();
                }
                if (msg.arg1 == Const.ARG1_IS_REFRESH_LOAD) {//刷新
                    noteAdapter.getData().clear();
                    noteAdapter.addData(0, replyNotes);
                } else {
                    noteAdapter.addData(replyNotes);
                }
                break;
            case MSG_NEXT_DATA:
                List<ReplyNote> noteList = noteAdapter.getData();
                if (noteList.size() > 0) {
                    getData(noteList.get(noteList.size() - 1).getNTime(), false);
                }
                break;
        }
    }
}
